
/**
 * form表单
 */
var Form = {
	formId:'',//表单id
	formType:'',//表单类型，search查询,ins新增,upd修改
	formTables:null,
	buttons:null,
	resObj:null,
	tabPage:null,
	
	/**
	 * 显示form
	 */
	init:function(){
		if(!Form.formId||!Form.formTables||!Form.formType){
			return;
		}
		Form.initForm();
		Form.initButton();
//		Form.initData();
	},
	/**
	 * 初始化form表格
	 */
	initForm:function(){
		var ftSign = "";
		if(Form.formType=='ins'||Form.formType=='upd'){
			ftSign = "col";
		}else if(Form.formType=='search'){
			ftSign = "cnd";
		}
		var tableHtml = "<table class='formTable' width='100%' id='table_"+Form.formId+"'>";
		tableHtml += "</table>";
		$("#"+Form.formId).append(tableHtml);
		var formTableObject = document.getElementById("table_"+Form.formId);
		$.each(Form.formTables,function(i,formTable){
			FormTable.tableId = Form.formId+"_"+i;
			var ftPre = "";
			if(formTable.ftSign){
				ftPre =  formTable.ftSign ;
			}
			if(ftSign){
				if(ftPre){
					ftPre = ftPre + "_" + ftSign;
				}else{
					ftPre = ftSign;
				}
			}
			FormTable.ftSign = ftPre;
			FormTable.resType = formTable.ftType;
			FormTable.arrayFmt = formTable.arrayFmt;
			FormTable.fields = formTable.elemTagList;
			FormTable.dicsMap = formTable.dicsMap;
			FormTable.data = formTable.data;
			var tableHtml = FormTable.createHtml();
			var initDataHtml = FormTable.createInitData();
			if(initDataHtml){
				tableHtml += initDataHtml;
			}
			
			var tableRow = formTableObject.insertRow();
			var tableCell = tableRow.insertCell();
			tableCell.innerHTML = tableHtml
			if(FormTable.resType==2){
				FormTable.createListTable()
//				$('#'+FormTable.tableId).datagrid();
			}
			FormTable.validateForm();
		});
		/*
		Form.toMapField();
		if(Form.tabPage&&Form.tabPage.length>0){
			Form.createTabForm();
		}else{
			var tableObject = $("#"+Form.formId).find('table');
			var formTable = "";
			if(ToolsUtil.isNotNull(Form.resObj.arrayFmt)){
				formTable = Form.createFmtFormTable(fmtField);
			}else{
				formTable = Form.createFormTable();
			}
			$(tableObject).append(formTable);
			Form.validateForm();
		}
		*/
	},
	/**
	 * 验证
	 */
	validate:function(){
//		$.each(Form.formTables,function(i,formTable){
//			if(FormTable.resType==2){
//				$('#'+Form.formId+"_"+i).datagrid("acceptChanges");
//			}
//		});
		var validateResult = $('#'+Form.formId).form('validate');
		return validateResult;
	},
	getFormData:function(){
		var dataJson = {};
		$.each(Form.formTables,function(i,formTable){
			if(formTable.ftType==2){
				$('#'+Form.formId+"_"+i).datagrid('endEdit',0);
				var d = $('#'+Form.formId+"_"+i).datagrid('getData');
				console.info(d);
			}else{
				var tableDataJson = $("#"+Form.formId).serializeJson();
				$.extend(dataJson, tableDataJson);
			}
		});
		return dataJson;
	},
	/**
	 * 显示form按钮
	 */
	initButton:function(){
		if(Form.formType=='search'){
			Form.initSearchButton();
		}
	},
	initSearchButton:function(){
		var buttonHtml = "<td>";
		buttonHtml += "<a href=\"javascript:void(0)\" id=\"searchButton\" class=\"easyui-linkbutton my-search-button\" iconCls=\"icon-search\" plain=\"true\">查询</a>";
		buttonHtml += "<a href=\"javascript:void(0)\" id=\"resetButton\" class=\"easyui-linkbutton my-search-button\" iconCls=\"icon-reset\" plain=\"true\">重置</a>";
		buttonHtml += "</td>";
		$("#"+Form.formId).find('table tr:last').append(buttonHtml);
		$('#searchButton').linkbutton();
		$('#resetButton').linkbutton();
		$("#searchButton").click(function(){
			if(typeof Form.resObj.search =='function'){
				Form.resObj.search();
			}
//			search();
		});
		$("#resetButton").click(function(){
			$("#"+Form.formId).form('reset');
//			$("#"+Form.formId)[0].reset();
		});
	}
}



